<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                              |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\AuctionOrder;

use AuctionBack\Logic\ConsultLogic;
use AuctionBack\Logic\OwnerConfirmLogic;
use AuctionCore\CarResource\Car;
use AuctionCore\CarResource\CarModel;
use AuctionCore\Other\Address;
use AuctionCore\ChangeOrder\ChangeOrder;
/**
 * 复检类
 *
 * @author Angl
 */
class Recheck
{
    /**
     * 新增复检记录
     *
     * @param $data
     *
     * @return mixed
     */
    public static function create($data)
    {
        $data['create_time'] = date('Y-m-d H:i:s');
        $id = M('recheck')->add($data);
        return $id;
    }

    /**
     * 更新复检信息
     *
     * @param $order_id
     * @param $data
     *
     * @return
     */
    public function update_by_order_id($order_id, $data)
    {
        if ($order_id < 1 || empty($data)) return false;
        return M('recheck')->where('order_id=' . $order_id)->save($data);
    }

    /**
     * 获取复检详细信息
     *
     * @param $order_id
     *
     * @return bool|mixed
     */
    public static function get_detail_by_order_id($order_id)
    {
        if (intval($order_id) < 1) return false;
        return M('recheck')->where("order_id=" . $order_id)->find();
    }

    /**
     * 获取复检详细信息
     *
     * @param $car_id
     *
     * @return bool|mixed
     */
    public static function get_detail_by_car_id($car_id)
    {
        if (intval($car_id) < 1) return false;
        return M('recheck')->where("car_id=" . $car_id)->find();
    }

    /**
     * 获取列表
     *
     * @param $page
     * @param $limit
     * @param $params [$status, $location_area, $order_no, $checker, $driver, $driver_phone]
     *
     * @return array
     */
    public function get_list($page, $limit, $params = array())
    {
        $page = intval($page) > 0 ? intval($page) : 1;
        $limit = intval($limit) > 0 ? intval($limit) : 10;
        $map = [];

        //状态限制
        if ($params['status'] == 0) {
            $map['arc.status'] = intval($params['status']);
            $map['o.status'] = array(array('eq', 7), array('eq', 8), 'or');
            $orderBy = "arc.create_time DESC";
        } elseif ($params['status'] == 1) {
            $map['arc.status'] = intval($params['status']);
            $map['o.status'] = array(array('eq', 7), array('eq', 8), 'or');
            $orderBy = "arc.next_trace_time DESC";
        } elseif ($params['status'] == 2 ) { //车况相符
            $map['arc.status'] = 2;
            $map['arc.is_car_condition_conform'] = 1;
            $orderBy = "arc.recheck_time DESC";
        }elseif ($params['status'] == 3){ //车况不符
            $map['arc.status'] = 2;
            $map['arc.is_car_condition_conform'] = 2;
            $orderBy = "arc.recheck_time DESC";
        }else{
            return [];
        }
        //车辆所在地
        if (!empty($params['location_area'])) {
            $map['c.location_area'] = array("in", $params['location_area']);
        }
        //拍单
        if (!empty($params['order_no'])) {
            $map['o.order_no'] = trim($params['order_no']);
        }
        //评估师
        if (!empty($params['checker'])) {
            $checker_id = M('employee')->where("real_name='" . trim($params['checker']) . "'")->getField('emp_id');
            $map['co.checker_id'] = $checker_id;
        }
        //车主姓名
        if (!empty($params['seller_name'])) {
            $map['co.seller_name'] = ['like', "%" . trim($params['seller_name']) . "%"];;
        }
        //车主电话
        if (!empty($params['seller_phone'])) {
            $map['co.phone'] = trim($params['seller_phone']);
        }

        $cols = "o.order_id,o.status as order_status,o.order_no,o.is_lock,o.is_consult,c.location_area,co.checker_id,co.seller_name,co.phone,
            c.car_id,c.brand_id,c.series_id,c.model_id,c.plate_prefix,c.plate_suffix,arc.status,c.car_source";
        $prefix = C("DB_PREFIX");
        $total = M('recheck')->alias("arc")
            ->join($prefix . "order as o ON o.order_id=arc.order_id")
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->where($map)->count();

        $list = M('recheck')->alias("arc")->field($cols)
            ->join($prefix . "order as o ON o.order_id=arc.order_id")
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->where($map)->page($page)->limit($limit)->order($orderBy)->select();
        //echo M()->getLastSql();
        if (!empty($list)) {
            $cm = new CarModel();
            $addr = new Address();
            foreach ($list as $key => $vl) {
                if($vl['is_consult']==0){
                    $dealtData = OwnerConfirmLogic::getDealtByOrderId($vl['order_id']);
                }else{
                    $dealtData = ConsultLogic::getDealtByOrderId($vl['order_id']);
                    if(empty($dealtData)){
                        $dealtData = OwnerConfirmLogic::getConsultByOrderId($vl['order_id']);
                    }
                }
                $list[$key]['owner_confirm_time'] = $dealtData['createtime'];
                //车型
                $brand = $cm->get_model_by_id($vl['brand_id'], 'brand');
                $series = $cm->get_model_by_id($vl['series_id'], 'series');
                $model = $cm->get_model_by_id($vl['model_id']);
                $list[$key]['car_model'] = $brand['name'] . $series['name'] . $model['name'];
                //所在地
                if ($vl['location_area']) {
                    $location_area = $addr->get_name_by_code($vl['location_area']);
                    $list[$key]['location_area'] = $location_area;
                } else {
                    $list[$key]['location_area'] = '无';
                }
                //是否显示违约
                $list[$key]['is_again_void'] = true;
                // $changeInfo = changeOrder::detailMap(['order_id'=>$vl['order_id'],'order_status'=>$vl['order_status']]);
                // if ($changeInfo) {
                //     $list[$key]['is_again_void'] = false;
                // }
                if ($vl['is_lock'] == 1) {
                    $list[$key]['is_again_void'] = false;
                }
                if ($vl['car_source'] == 2) {
                    $list[$key]['is_again_void'] = false;
                }

                //车牌号
                $list[$key]['plate_number'] = C("PLATE_PREFIX." . $vl['plate_prefix']) . $vl['plate_suffix'];
                //评估师
                if ($vl['status'] < 2 || $vl['checker_id'] > 0) {
                    $checker = M("employee")->where("emp_id=" . $vl['checker_id'])->find();
                    $list[$key]['checker_name'] = $checker['real_name'];
                }
            }
        }
        $data = ['total' => $total, 'list' => $list];
        return $data;
    }

    /**
     * 记录复检跟踪日志
     *
     * @param $order_id
     * @param $emp_id
     * @param $rechecker
     * @param $action
     * @param $remark
     * @param $create_time
     *
     * @return mixed
     */
    public static function save_trace_log($order_id, $emp_id, $rechecker, $action, $remark, $create_time)
    {
        $log = [
            'order_id' => $order_id,
            'emp_id' => $emp_id,
            'rechecker' => $rechecker,
            'action' => $action,
            'remark' => $remark,
            'create_time' => $create_time
        ];
        return M('recheck_trace_log')->add($log);
    }

    /**
     * 获取跟踪日志
     *
     * @param $order_id
     *
     * @return mixed
     */
    public static function get_trace_log($order_id)
    {
        return M("recheck_trace_log")->where("order_id=" . $order_id)->order('create_time desc')->select();
    }

    /**
     * 获取评估师信息
     *
     * @return mixed
     */
    public function get_checker_info($checkerID)
    {
        //要查询的详情
        $cols = "e.emp_id,e.real_name,e.mobile";
        $prefix = C('DB_PREFIX');
        $cond = "e.emp_id='" . $checkerID . "'";
        $res = M('employee')->alias('e')->field($cols)->where($cond)->find();
// return M('order')->getLastSql();
        return $res;
    }

    /**
     * 根据sid获取复检信息
     * @param $sid
     * @return mixed
     */
    public static function getRecheckInfoBySid($sid)
    {
        $map = [
            'sid' => $sid
        ];
        $recheckInfo = M("recheck")->where($map)->find();
        $recheckInfo['condition_checker_name'] = M("employee")->where(['emp_id'=>$recheckInfo['condition_checker']])->getField("real_name");
        $recheckInfo['procedure_checker_name'] = M("employee")->where(['emp_id'=>$recheckInfo['procedure_checker']])->getField("real_name");
        return $recheckInfo;
    }


}

//end class
